﻿Imports Mystery

Public Class DayOfWeeksControl
    Implements IPropertyControl

    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentProperty

    Private Shared map As New Dictionary(Of String, DayOfWeek)

    Shared Sub New()
        map("cbLun") = DayOfWeek.Monday
        map("cbMar") = DayOfWeek.Tuesday
        map("cbMer") = DayOfWeek.Wednesday
        map("cbGio") = DayOfWeek.Thursday
        map("cbVen") = DayOfWeek.Friday
    End Sub


    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentProperty)()
            _label = _content_property.label
            _property_info = value
        End Set
    End Property

    Private Function getCheckBoxes() As IEnumerable(Of CheckBox)
        Return New List(Of CheckBox) From {cbLun, cbMar, cbMer, cbGio, cbVen}
    End Function

    Private Function getAsString() As String
        Dim wd As IEnumerable(Of DayOfWeek) = From x As CheckBox In getCheckBoxes() Where x.IsChecked Select map(x.Name)
        Dim values As New List(Of String)(From x As DayOfWeek In wd Select CStr(x))
        Return String.Join(",", values.ToArray)
    End Function

    Private Sub setFromString(input As String)
        Dim values() As String = If(String.IsNullOrEmpty(input), {}, input.Split(","))
        For Each cb As CheckBox In getCheckBoxes()
            cb.IsChecked = values.Contains(map(cb.Name))
        Next
    End Sub

    Public Property value As Object Implements IPropertyControl.value
        Get
            Return getAsString()
        End Get
        Set(value As Object)
            If TypeOf (value) Is ContentPropertyValue Then value = DirectCast(value, ContentPropertyValue).getValue
            Dim new_value As String = value
            If new_value = getAsString() Then Exit Property
            setFromString(new_value)
        End Set
    End Property



    Public Property typed_value As String
        Get
            Return value
        End Get
        Set(value As String)
            Me.value = value
        End Set
    End Property

    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property


    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return GridLength.Auto
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Dim v As New ContentPropertyValue(Of String)(typed_value)
        _content_property.save()(_content, v)
        Return New List(Of IContent) From {_content}
    End Function


    Public Event valueChanged() Implements IPropertyControl.valueChanged

    Private Sub tbValue_TextChanged() Handles cbLun.Checked, cbMar.Checked, cbMer.Checked, cbGio.Checked, cbVen.Checked,
        cbLun.Unchecked, cbMar.Unchecked, cbMer.Unchecked, cbGio.Unchecked, cbVen.Unchecked

        RaiseEvent valueChanged()
    End Sub
End Class
